unit MesaDM;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ZDataset, ZSqlUpdate, ZSequence, MainDM, QueryConditions, db;

type

  { TMesaData }

  TMesaData = class(TMainDataModule)
    MesaDatasource: TDatasource;
    MesaZSequence: TZSequence;
    MesaZUpdate: TZUpdateSQL;
    MesaZQuery: TZQuery;
    MesaZQuerydesc_mesa: TStringField;
    MesaZQueryid_mesa: TLargeintField;
    MesaZQuerystatus: TStringField;
    AuxiliarZQuery: TZQuery;
    procedure DataModuleCreate(Sender: TObject);
    /// Abre o DataSet
    procedure OpenDataSet(); override;
    /// Fecha o DataSet
    procedure CloseDataSet(); override;
    /// Realiza busca
    /// @param(Condicoes são as condições para a busca)
    /// @param(Tabela é a tabela no banco de dados)
    procedure Search(Condicoes: TQueryConditions; Tabela: string); override;
    function BuscaData():string;
    function BuscaUsuario(IDUsuario:integer):string;
    function VerificaCaixaAberto(): string;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  MesaData: TMesaData;

implementation

uses ConnectionDM;

{ TMesaData }


procedure TMesaData.DataModuleCreate(Sender: TObject);
begin
   MainDataSet := MesaZQuery;
   MesaZQuery.Open;
end;
procedure TMesaData.OpenDataSet;
begin
  //ConnectionData.EmptyQuery(MesaZQuery, 'mesa');
end;

procedure TMesaData.CloseDataSet;
begin
  //MesaZQuery.Close();
end;

procedure TMesaData.Search(Condicoes: TQueryConditions; Tabela: string);
begin
    ConnectionData.OpenQueryConditions(MesaZQuery, Tabela, Condicoes);
end;

function TMesaData.BuscaData(): string;
var
  Data: string;
begin
  with AuxiliarZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('SELECT current_date as date');
    Open();
    Data := FieldByName('date').AsString;
    Result := Data;
  end;
end;

function TMesaData.BuscaUsuario(IDUsuario: integer): string;
var
  Data: string;
begin
  with AuxiliarZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('SELECT login FROM usuario where id_usuario=:id_usuario');
    ParamByName('id_usuario').AsInteger:=IDUsuario;
    Open();
    Data := FieldByName('login').AsString;
    Result := Data;
  end;
end;

function TMesaData.VerificaCaixaAberto: string;
begin
   with AuxiliarZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('Select id_caixa_movimentacao');
    SQL.Add('FROM caixa_movimentacao');
    SQL.Add('where status=:status');
    ParamByName('status').AsString := 'A';
    Open();
    if (RecordCount > 0) then begin
      Result := FieldByName('id_caixa_movimentacao').AsString;
    end else begin
      Result := '';
    end;
  end;
end;

initialization
  {$I mesadm.lrs}

end.

